﻿using VisionDesigner.CircleFind;
using VM.Common.Helper;
using System;
using System.Collections.Generic;

namespace VM.Modules.CircleFinding
{
    /// <summary>
    /// 圆检测参数模型，提供友好的UI参数管理
    /// </summary>
    [Serializable]
    public class ParamModel : NotifyPropertyBase
    {
        #region 常量和映射

        /// <summary>
        /// 中文参数名称到英文值的映射字典
        /// </summary>
        public static readonly Dictionary<string, string> ParamMap = new Dictionary<string, string>
        {
            // 方向参数
            { "由内向外", "InnerToOuter" },
            { "由外向内", "OuterToInner" },
            
            // 圆类型参数
            { "查找最佳圆", "Best" },
            { "查找最大的圆", "Largest" },
            { "查找最小的圆", "Smallest" },
            
            // 边缘过渡参数
            { "由黑到白", "BlackToWhite" },
            { "由白到黑", "WhiteToBlack" },
            { "两种类型", "Both" },
            
            // 拟合算法参数
            { "最小二乘", "LS" },
            { "Huber函数", "Huber" },
            { "Tukey函数", "Tukey" },
            { "一次全局最小二乘", "ALS" },
            { "提取局部最小二乘,取最佳", "LLS" }
        };

        #endregion

        /// <summary>
        /// 底层圆查找工具
        /// </summary>
        internal CCircleFindTool cCircleFindTool;
        /// <summary>
        /// 设置圆查找工具
        /// </summary>
        public CCircleFindTool CircleFindTool
        {
            set
            {
                cCircleFindTool = value;

                if (cCircleFindTool != null)
                {
                    // 初始化所有参数
                    cCircleFindTool.SetRunParam("MinRadius", MinRadius.ToString());
                    cCircleFindTool.SetRunParam("MaxRadius", MaxRadius.ToString());
                    cCircleFindTool.SetRunParam("EdgeThresh", EdgeThresh.ToString());
                    cCircleFindTool.SetRunParam("RadNum", RadNum.ToString());
                    cCircleFindTool.SetRunParam("ProLength", ProLength.ToString());
                    cCircleFindTool.SetRunParam("CircleFindMode", ParamMap[CircleFindMode.ToString()]);
                }
            }
            get => cCircleFindTool;
        }

        #region 圆检测参数

        private int _MinRadius = 10;
        /// <summary>
        /// 最小半径（像素）
        /// </summary>
        public int MinRadius
        {
            get => _MinRadius;
            set
            {
                Set(ref _MinRadius, value);
                cCircleFindTool?.SetRunParam("MinRadius", value.ToString());
            }
        }

        private int _MaxRadius = 1000;
        /// <summary>
        /// 最大半径（像素）
        /// </summary>
        public int MaxRadius
        {
            get => _MaxRadius;
            set
            {
                Set(ref _MaxRadius, value);
                cCircleFindTool?.SetRunParam("MaxRadius", value.ToString());
            }
        }

        private int _EdgeThresh = 5;
        /// <summary>
        /// 边缘检测阈值（梯度幅值）
        /// </summary>
        public int EdgeThresh
        {
            get => _EdgeThresh;
            set
            {
                Set(ref _EdgeThresh, value);
                cCircleFindTool?.SetRunParam("EdgeThresh", value.ToString());
            }
        }

        private int _RadNum = 30;
        /// <summary>
        /// 卡尺数量（圆周上的测量点数）
        /// </summary>
        public int RadNum
        {
            get => _RadNum;
            set
            {
                Set(ref _RadNum, value);
                cCircleFindTool?.SetRunParam("RadNum", value.ToString());
            }
        }

        private int _ProLength = 5;
        /// <summary>
        /// 卡尺宽度（像素）
        /// </summary>
        public int ProLength
        {
            get => _ProLength;
            set
            {
                Set(ref _ProLength, value);
                cCircleFindTool?.SetRunParam("ProLength", value.ToString());
            }
        }

        /// <summary>
        /// 可用的圆查找模式列表
        /// </summary>
        public Array CircleFindModeArray => Enum.GetValues(typeof(CircFindMode));

        private CircFindMode _CircleFindMode = CircFindMode.查找最佳圆;
        /// <summary>
        /// 当前选择的圆查找模式
        /// </summary>
        public CircFindMode CircleFindMode
        {
            get => _CircleFindMode;
            set
            {
                Set(ref _CircleFindMode, value);
                cCircleFindTool?.SetRunParam("CircleFindMode", ParamMap[value.ToString()]);
            }
        }

        #endregion
    }

    /// <summary>
    /// 圆检测可用模式
    /// </summary>
    [Serializable]
    public enum CircFindMode
    {
        /// <summary>查找半径最小的圆</summary>
        查找最小的圆,

        /// <summary>查找半径最大的圆</summary>
        查找最大的圆,

        /// <summary>根据拟合质量查找最佳圆</summary>
        查找最佳圆
    }
}